package cn.edu.leetcode.买卖股票的最佳时机;

public class 买卖股票的最佳时机1 {
    public static int maxProfit(int[] prices) {
        int max = prices[0], min = prices[0]; // 最高价钱和最低价钱
        int maxProfit = 0;  //最大利润
        for (int price : prices) {
            if (price > max){
                max = price;
                maxProfit = Math.max(maxProfit, max-min);
            }
            if (price < min) {
                max = price;
                min = price;
            }
        }
        return maxProfit;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }
}
